﻿/*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*
 *~*  16-Oct-2009
 *~*  www.Learning-Leisure.com
 *~*  Project : PB.FBManager                    Namespace : PB.FBManager
 *~*  File : FBHelper.cs                            Class : FBHelper
 *~*  Author : Pinal Bhatt 
 *~*            self@P-Bhatt.com, www.P-Bhatt.com
 *~*   Version History
 *~*            Last Update Date :     Date 16-Oct-2009       Pinal Bhatt
 *~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*/

using System.Web;

namespace PB.FBManager
{
    /// <summary>
    /// Collection of Helper Methods for Facebook Manager component
    /// </summary>
    public class FBHelper
    {
        /// <summary>
        /// Reads Facebook session cookie
        /// </summary>
        /// <param name="cookieName"></param>
        /// <returns></returns>
        internal static string GetFacebookCookie(string cookieName)
        {
            string retString = null;
            if (!string.IsNullOrEmpty(cookieName))
            {
                string fullCookieName = FBApp.ApiKey + "_" + cookieName;

                if (HttpContext.Current != null && HttpContext.Current.Request.Cookies[fullCookieName] != null)
                    retString = HttpContext.Current.Request.Cookies[fullCookieName].Value;
            }
            return retString;
        }

        /// <summary>
        /// Gets facebook.Schema.user object for currently loged in user
        /// </summary>
        /// <returns></returns>
        public static facebook.Schema.user GetFBUser()
        {
            FBSession fbSession = new FBSession();
            if (fbSession.IsConnected())
                return GetFBUser(fbSession.UserID);
            else
                return null;
        }

        /// <summary>
        /// Gets facebook.Schema.user object for specified user
        /// </summary>
        /// <param name="fbUserId">Facebook userid</param>
        /// <returns></returns>
        public static facebook.Schema.user GetFBUser(long fbUserId)
        {
            facebook.Schema.user fbUser = null;
            FConnectAPI fbApi = new FConnectAPI(true, true);
            if (fbApi != null && fbUserId > 0)
                fbUser = fbApi.users.getInfo(fbApi.uid);
            return fbUser;            
        }
    }
}
